package com.borko.healthcare.utils.beans;

import java.util.ArrayList;
import java.util.List;

import com.borko.healthcare.entities.interfaces.MedicineOrder;
import com.borko.healthcare.utils.interfaces.Cart;

public class CartBean implements Cart {

	private List<MedicineOrder> medicineOrders;
	private double totalPrice;
	
	public CartBean() {
		medicineOrders = new ArrayList<MedicineOrder>();
		totalPrice = 0;
	}
	
	public void remove(MedicineOrder medicineOrder) {
		medicineOrders.remove(medicineOrder);
		updatePrice();
		medicineOrder.setQty(0);
	}
	
	public void addMedicineOrder(MedicineOrder medicineOrder, int qty) {
		if (medicineOrders.contains(medicineOrder)) {
			int index = medicineOrders.indexOf(medicineOrder);
			medicineOrders.get(index).setQty(medicineOrder.getQty() + qty);
			medicineOrders.get(index).updatePrice();
		} else {
			medicineOrder.setQty(qty);
			medicineOrder.updatePrice();
			medicineOrders.add(medicineOrder);
		}
		
		updatePrice();
	}
	
	public List<MedicineOrder> getMedicineOrders() {
		return medicineOrders;
	}

	public void setMedicineOrders(List<MedicineOrder> medicineOrders) {
		this.medicineOrders = medicineOrders;
	}

	public void updatePrice() {
		totalPrice = 0;
		for (MedicineOrder medicineOrder : medicineOrders) {
			totalPrice += medicineOrder.getPrice();
		}
	}

	public double getTotalPrice() {
		return totalPrice;
	}

	public void setTotalPrice(double totalPrice) {
		this.totalPrice = totalPrice;
	}

}
